package cn.lxh.org.controller;


import cn.lxh.basic.utils.PageList;
import cn.lxh.org.basic.utils.AjaxResult;
import cn.lxh.org.domain.Department;
import cn.lxh.org.query.DepartmentQuery;
import cn.lxh.org.service.IDepartmentService;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@RequestMapping("/dept")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;
    @NacosValue(value = "${useLocalCache}",autoRefreshed = true)
    private String name3;


    @GetMapping("nacos")
    public String queryBy(){

        System.out.println(name3);

        return name3 ;
    }


    /**
     * 查询一条 数据  GET
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Department queryById(@PathVariable("id") Long id){
        return departmentService.queryById(id);
    }

    /**
     * 查询全部
     * @return
     */
    @GetMapping
    public List<Department> queryAll(){
        List<Department> departments = departmentService.queryAll();
        return departments;
    }

    /**
     * 删除
     * @param id
     * @return
     */
   @DeleteMapping("/{id}")
    public AjaxResult deleteById(@PathVariable("id") Long id){
        try {
            departmentService.delete(id);
            System.out.println("开始");
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me();
        }
    }

    /**
     * 增加 修改
     * @param department
     * @return
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            if (department.getId() == null){
                departmentService.save(department);
            }else{
                departmentService.update(department);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作异常").setSuccess(false);
        }
    }

    /**
     * 分页功能
     * @param departmentQuery
     * @return
     *
     */
    @PostMapping
    public PageList queryPage(@RequestBody DepartmentQuery departmentQuery){
        return departmentService.queryList(departmentQuery);
    }
    /**
     * 批量删除
     */
    @PatchMapping
    public AjaxResult batchRemove(@RequestBody Long[] ids){
        try {
            departmentService.batchReove(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult();
        }
    }

    @GetMapping("/tree")
    public List<Department> queryTree(){
        return departmentService.queryTree();
    }


}
